package com.fau.wmbj.server.audit;

import com.fau.wmbj.server.entity.AuditLog;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 审计日志自定义参数解析器-Audit
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2019/7/29 11:55
 **/
@Component
public class AuditLogMethodArgumentResolver implements HandlerMethodArgumentResolver {

    /**
     * 支持参数
     *
     * @param methodParameter MethodParameter
     * @return 是否支持参数
     */
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return AuditLog.class.isAssignableFrom(methodParameter.getParameterType());
    }

    /**
     * 解析变量
     *
     * @param methodParameter       MethodParameter
     * @param modelAndViewContainer ModelAndViewContainer
     * @param nativeWebRequest      NativeWebRequest
     * @param webDataBinderFactory  WebDataBinderFactory
     * @return 变量
     */
    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        return nativeWebRequest.getAttribute(AuditLog.AUDIT_LOG_ATTRIBUTE_NAME, RequestAttributes.SCOPE_REQUEST);
    }

}